package com.markerhub.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.markerhub.entity.Comment;
import com.markerhub.entity.User;
import com.markerhub.mapper.CommentMapper;
import com.markerhub.mapper.UserMapper;
import com.markerhub.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.markerhub.views.CommentView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2021-04-10
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    UserMapper userMapper;

    public List<CommentView> Shanan(List<Comment> comments)
    {
        List<CommentView> res = new ArrayList<>();
        for(Comment comment:comments)
        {
            CommentView commentView=new CommentView(comment);
            res.add(commentView);
        }
        return res;
    }

    @Override
    public List<CommentView> getCommentList(Integer blogId)
    {
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("blog_id",blogId).eq("parent_comment_id",-1);
        List<Comment> comments=commentMapper.selectList(queryWrapper);
        List<CommentView> commentViewlet= Shanan(comments);
        for(CommentView comment: commentViewlet)
        {
            QueryWrapper<Comment> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("parent_comment_id",comment.getCommentId());
            List<Comment> comments2=commentMapper.selectList(queryWrapper2);
            List<CommentView> commentViewlet2= Shanan(comments2);
            for(CommentView com: commentViewlet2)
            {
                QueryWrapper<Comment> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("parent_comment_id",com.getCommentId());
                List<Comment> comments3=commentMapper.selectList(queryWrapper3);
                List<CommentView> commentViewlet3= Shanan(comments3);
                com.setChildren(commentViewlet3);
            }
            comment.setChildren(commentViewlet2);
        }
        return commentViewlet;
    }

    @Override
    public void deleteComment(Integer commentId,Integer blogId)
    {
        QueryWrapper<Comment> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("blog_id",blogId).eq("comment_id",commentId);
        List<Comment> comments=commentMapper.selectList(queryWrapper);
        for(Comment com: comments)
        {
            QueryWrapper<Comment> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("blog_id",blogId).eq("parent_comment_id",com.getCommentId());
            List<Comment> comments2=commentMapper.selectList(queryWrapper2);
            for(Comment co: comments2)
            {
                QueryWrapper<Comment> queryWrapper3=new QueryWrapper<>();
                queryWrapper3.eq("blog_id",blogId).eq("parent_comment_id",co.getCommentId());
                commentMapper.delete(queryWrapper3);
            }
            commentMapper.delete(queryWrapper2);
        }
        commentMapper.delete(queryWrapper);
    }

    @Override
    public void addComment(Integer uid,Integer blogId,String content,Integer parentCommentId)
    {
        User user=userMapper.selectById(uid);
        Comment comment=new Comment();
        comment.setBlogId(blogId);
        Date data=new Date();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(data.toInstant(), zoneId);
        comment.setCreateTime(localDateTime);
        comment.setContent(content);
        comment.setParentCommentId(parentCommentId);
        comment.setUid(uid);
        comment.setNickname(user.getNickname());
        comment.setAvatar(user.getAvatar());
        commentMapper.insert(comment);
    }
}
